package com.ecarx.vhaltests.hy11.testcases.climate;
import static com.ecarx.xui.adaptapi.car.hvac.IFragrance.HVAC_FUNC_AIR_FRAGRANCE_LOW;
import static org.junit.Assert.assertEquals;
import android.car.VehicleAreaType;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;
import androidx.test.platform.app.InstrumentationRegistry;
import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VhalUnitTestBase;
import com.ecarx.xui.adaptapi.car.IWrapper;
import org.junit.BeforeClass;
import org.junit.Test;

public class HvacFuncAirFragranceLowTest extends VhalUnitTestBase {
    private static IWrapper.IPropertyId propertyId;
    protected static IWrapper wrapper;
    private static IpcpClient ipcpClient = new IpcpClient();
    private static final int AREA_ID = VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;
    private static int[] cbkPropValue;
    private static int cbkPropStatus;

    @BeforeClass
    public static void testInit() {
        wrapper = com.ecarx.xui.adaptapi.car.Car.createWrapper(InstrumentationRegistry.getInstrumentation().getTargetContext());
        propertyId = wrapper.getWrappedPropertyId(IWrapper.WrappedIdType.ID_TYPE_FUNCTION, HVAC_FUNC_AIR_FRAGRANCE_LOW);
        setConfig();
    }

    public static void setConfig(){
        int cemSvcId = 37;
        int[] cemOperId = {999};
        boolean cemResult = ipcpClient.setConfig(cemSvcId, cemOperId);
        assertEquals(true, cemResult);
        delay();
    }

    public void mockFragChUseUpWrnFrmMcu(int fragCh1UseUpWrn, int fragCh2UseUpWrn, int fragCh3UseUpWrn){
        int cemSvcId = 37;
        int cemOperId = 999;
        String[] cems = {"fragCh1UseUpWrn","fragCh2UseUpWrn","fragCh3UseUpWrn"};
        //NoWarn(0), Warn(1)
        int[] cemValues = {fragCh1UseUpWrn, fragCh2UseUpWrn, fragCh3UseUpWrn};
        boolean cemResult = ipcpClient.setCEMSignal(cemSvcId, cemOperId, cems, cemValues);
        delay();
        assertEquals(true, cemResult);
    }


    @Test
    public void testSlot1UseUpWarn(){
        mockFragChUseUpWrnFrmMcu(1,0,0);
        delay();
        int[] res = mCarPropertyMgr.getIntArrayProperty(propertyId.getPropertyId(), AREA_ID);
        assertEquals(1, res[0]);
    }

    @Test
    public void testSlot2UseUpWarn(){
        mockFragChUseUpWrnFrmMcu(1,1,0);
        delay();
        int[] res = mCarPropertyMgr.getIntArrayProperty(propertyId.getPropertyId(), AREA_ID);
        assertEquals(1, res[0]);
        assertEquals(1, res[1]);
    }

    @Test
    public void testSlot3UseUpWarn(){
        mockFragChUseUpWrnFrmMcu(1,1,1);
        delay();
        int[] res = mCarPropertyMgr.getIntArrayProperty(propertyId.getPropertyId(), AREA_ID);
        assertEquals(1, res[0]);
        assertEquals(1, res[1]);
        assertEquals(1, res[2]);
    }

    private static int[] toIntArray(Integer[] input) {
        int len = input.length;
        int[] arr = new int[len];

        for (int i = 0; i < len; ++i) {
            arr[i] = input[i];
        }
        return arr;
    }

    @Test
    public void testFragChUseUpWrnCallback(){
        CarPropertyManager.CarPropertyEventCallback carPropertyEventCallback = new CarPropertyManager.CarPropertyEventCallback() {
            @Override
            public void onChangeEvent(CarPropertyValue carPropertyValue) {
                cbkPropValue = toIntArray((Integer[]) carPropertyValue.getValue());
                cbkPropStatus = carPropertyValue.getStatus();
            }

            @Override
            public void onErrorEvent(int i, int i1) {

            }
        };

        mCarPropertyMgr.registerCallback(carPropertyEventCallback, propertyId.getPropertyId(), CarPropertyManager.SENSOR_RATE_ONCHANGE);
        mockFragChUseUpWrnFrmMcu(1,0,1);
        delay();
        assertEquals(0, cbkPropStatus);
        assertEquals(1, cbkPropValue[0]);
        assertEquals(0, cbkPropValue[1]);
        assertEquals(1, cbkPropValue[2]);
        mCarPropertyMgr.unregisterCallback(carPropertyEventCallback);
    }
}
